/**********************************************************
* Lasonde.fr
* Ver 1.0
* 22/04/2011
***********************************************************/

var url_core = "http://www.lasonde.fr/wp-content/plugins/lasonde/LSD_core";
var text_chargement = "Chargement du sondage...";
if (typeof (js_load) == 'undefined') {
	var js_load;
	var LSD_twenn_load;
	function LSD_init_conteneur(config) {
		var bloc_name = config.bloc_name;
		if (typeof (config.css_url) == "undefined") {
			config.css_url = false
		}
		var conteneur = "<div id='lsd_js_" + config.bloc_name
				+ "'></div><div id='lsd_css_" + config.bloc_name
				+ "'></div><div id='" + config.bloc_name
				+ "'><div id='LSD_loading_" + config.bloc_name
				+ "' style='display:none;'></div></div>";
		if (config.box) {
			document.getElementById(config.box).innerHTML = conteneur
		} else {
			document.write(conteneur)
		}
		get_sondage_css(config);
		LSD_load_tween("http://lasonde-javascript-hosting.googlecode.com/svn/trunk/LSD_tween.js");
		var params = "?step=1&bloc_name=" + config.bloc_name + "&sd_id="
				+ config.sd_id + "&tween=" + config.tween
				+ "&callback=Ajax_caller_sondage";
		lsd_call_ajax(url_core + '/bdd-sondages.php' + params)
	}
	function createRequestObject() {
		var http;
		if (window.XMLHttpRequest) {
			http = new XMLHttpRequest()
		} else if (window.ActiveXObject) {
			http = new ActiveXObject("Microsoft.XMLHTTP")
		}
		return http
	}
	function Ajax_caller_sondage(step, lsd) {
		switch (step) {
		case 1:
			if (lsd.html != '' || !null) {
				document.getElementById(lsd.bloc_name).innerHTML = lsd.html;
				if (lsd.restriction == 0) {
					if (document.getElementById("LSD_loading_" + lsd.bloc_name))
						document.getElementById("LSD_loading_" + lsd.bloc_name).style.display = 'none';
					if (document.getElementById("LSD_bouton_vote_"
							+ lsd.bloc_name))
						document.getElementById("LSD_bouton_vote_"
								+ lsd.bloc_name).onclick = function() {
							Ajax_caller_sondage(2, lsd);
							return false
						};
					if (document.getElementById("LSD_bouton_resultats_"
							+ lsd.bloc_name)) {
						document.getElementById("LSD_bouton_resultats_"
								+ lsd.bloc_name).onclick = function() {
							document.getElementById("LSD_loading_"
									+ lsd.bloc_name).style.display = '';
							lsd.html = 'ok_db';
							Ajax_caller_sondage(3, lsd);
							return false
						}
					}
				}
			} else {
				alert('L\'affichage du sondage a échoué.');
				document.getElementById("LSD_loading_" + lsd.bloc_name).style.display = 'none'
			}
			break;
		case 2:
			document.getElementById("LSD_loading_" + lsd.bloc_name).style.display = '';
			var div_checkboxs = document.getElementById('LSD_content_rep_'
					+ lsd.bloc_name).childNodes;
			var response_post = "?step=2&bloc_name=" + lsd.bloc_name
					+ "&sd_id=" + lsd.sd_id + "&tween=" + lsd.tween
					+ "&callback=Ajax_caller_sondage";
			var b = 0;
			for ( var i = 0; i < div_checkboxs.length; i++) {
				var checkboxs = div_checkboxs[i].childNodes;
				for ( var c = 0; c < checkboxs.length; c++) {
					if (checkboxs[c].checked == true) {
						response_post = response_post + "&" + checkboxs[c].name
								+ "=" + checkboxs[c].value;
						b++
					}
				}
			}
			if (b != 0) {
				lsd_call_ajax(url_core + '/bdd-sondages.php' + response_post)
			} else {
				alert('Veuillez sélectionner une réponse');
				document.getElementById("LSD_loading_" + lsd.bloc_name).style.display = 'none'
			}
			return false;
			break;
		case 3:
			if (lsd.html == 'ok_db') {
				var result_post = "?step=3&bloc_name=" + lsd.bloc_name
						+ "&sd_id=" + lsd.sd_id + "&tween=" + lsd.tween
						+ "&callback=Ajax_caller_sondage";
				lsd_call_ajax(url_core + '/bdd-sondages.php' + result_post)
			} else {
				alert('Erreur lors du vote.');
				document.getElementById("LSD_loading_" + lsd.bloc_name).style.display = 'none'
			}
			break;
		case 4:
			if (lsd.html != '') {
				document.getElementById(lsd.bloc_name).innerHTML = lsd.html;
				if (document.getElementById("LSD_loading_" + lsd.bloc_name))
					document.getElementById("LSD_loading_" + lsd.bloc_name).style.display = 'none';
				var y = 0;
				var div_LSD_rep_elems = document.getElementById('LSD_content_rep_'
						+ lsd.bloc_name).childNodes;
				var div_LSD_rep = document.getElementById('LSD_content_rep_'
						+ lsd.bloc_name).childNodes;
				for ( var i = 0; i < div_LSD_rep.length; i++) {
					var reponses = div_LSD_rep[i].childNodes;
					for ( var c = 0; c < reponses.length; c++) {
						if (typeof (reponses[c].id) != 'undefined') {
					    if(reponses[c].getAttribute("class") != "wp-smiley" && reponses[c].tagName != "IFRAME" && reponses[c].tagName != "BR" && reponses[c].tagName != "OBJECT"){
							y++;
							if (document.getElementById('LSD_rep_prct_'
									+ lsd.bloc_name + '_' + y)) {
								var prct = document
										.getElementById('LSD_rep_prct_'
												+ lsd.bloc_name + '_' + y).value;
								var largeur_total = document
										.getElementById('LSD_rep_barre_'
												+ lsd.bloc_name + '_' + y).offsetWidth;
								var largeur_barre = (prct * largeur_total) / 100;
								if (largeur_barre >= 0.1 && largeur_barre <= 1)
									largeur_barre = 1;
								largeur_barre = largeur_barre.toFixed(0);
								if (lsd.tween == 0
										|| typeof (lsd.tween) == 'undefined') {
									setStyle(reponses[c].id, "width",
											largeur_barre + "px")
								} else {
									lsd.tween = eval(lsd.tween);
									var t1 = new Tween(
											document
													.getElementById(reponses[c].id).style,
											"width", lsd.tween, 0,
											largeur_barre, 4, "px");
									t1.start()
								}
							}
						}
						}
					}
				}
			} else {
				alert('L\'affichage des résultats a échoué.')
			}
			return false;
			break
		}
	}
	function get_sondage_css(config) {
		if (config.css_url != false) {
			loadjscssfile(config.css_url, "css", config.bloc_name)
		} else if (typeof (config.css_id) != 'undefined') {
			var css_post = "?step=4&bloc_name=" + config.bloc_name + "&css_id="
					+ config.css_id + "&sd_id=" + config.sd_id;
			loadjscssfile(url_core + '/bdd-sondages.php' + css_post, "css",
					config.bloc_name)
		}
	}
	function setStyle(objId, style, value) {
		document.getElementById(objId).style[style] = value
	}
	function loadjscssfile(filename, filetype, bloc_name) {
		if (filetype == "js") {
			var fileref = document.createElement('script');
			fileref.setAttribute("type", "text/javascript");
			fileref.setAttribute("src", filename);
			document.getElementById("lsd_js_" + bloc_name).innerHTML = "";
			document.getElementById("lsd_js_" + bloc_name).appendChild(fileref)
		} else if (filetype == "css") {
			var fileref = document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", filename);
			document.getElementById("lsd_css_" + bloc_name).innerHTML = "";
			document.getElementById("lsd_css_" + bloc_name)
					.appendChild(fileref)
		} else if (filetype == "ajax") {
			var fileref = document.createElement("script");
			fileref.setAttribute("rel", "lsd_ajax_caller");
			fileref.setAttribute("type", "text/javascript");
			fileref.setAttribute("src", filename);
			document.getElementsByTagName('head')[0].appendChild(fileref)
		}
	}
	function LSD_load_tween(filename) {
		head = document.getElementsByTagName('head')[0];
		if (!document.getElementById("lsd_tween_script")) {
			LSD_twenn_load = document.createElement("script");
			LSD_twenn_load.setAttribute("id", "lsd_tween_script");
			LSD_twenn_load.setAttribute("type", "text/javascript");
			LSD_twenn_load.setAttribute("src", filename);
			head.appendChild(LSD_twenn_load)
		}
	}
	function lsd_call_ajax(url) {
		loadjscssfile(url, "ajax")
	}
}